﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace NeteasyMusic
{
    class request
    {
        public static string Post(string url,string data)
        {
           
            //Byte[] postdata = Encoding.UTF8.GetBytes(data);
            //WebRequest req = WebRequest.Create(url) ;
            //req.ContentLength = data.Length;
            //req.Method = "Post";
            //Stream reqstream = req.GetRequestStream();
            //reqstream.Write(postdata, 0, data.Length);
            //reqstream.Close();
            
            //WebResponse response =req.GetResponse();
            //StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //string ret = sr.ReadToEnd();
            //sr.Close(); 
            
            //return ret;

            {
                //data
                //string cookieStr = "51fd9f14fa7561b5";
                //string postData = string.Format("userid={0}&password={1}", "guest", "123456");
                byte[] data2 = Encoding.UTF8.GetBytes(data);

                // Prepare web request...
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "Post";
                //request.Referer = "https://www.xxx.com";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
                //request.Host = "www.xxx.com";
                //request.Headers.Add("Cookie", cookieStr);
                request.ContentLength = data2.Length;
                Stream newStream = request.GetRequestStream();

                // Send the data.
                newStream.Write(data2, 0, data2.Length);
                newStream.Close();

                // Get response
                HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string content = reader.ReadToEnd();
                return content;
            }
        }
        public static string Get(string url)
        {
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.Method = "Get";
            Stream reqstream = req.GetRequestStream();
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string ret = sr.ReadToEnd();
            sr.Close();
            response.Close();
            reqstream.Close();
            return ret;
        }

    }
}
